/**
 * @file listener_CAN.cpp
 * @author circleup (circleup@foxmail.com)
 * @brief LCCAN 数据监听节点，订阅 CAN_receive_data 话题
 * @version v2.1
 * @date 2020-06-25
 * 
 * @copyright Copyright (c) 2020
 * 
 */

#include "ros_can_driver.h"

#include "ros_car/VCU_frame.h"
#include "ros/ros.h"

/**
 * @brief CAN 监听节点回调函数
 * 
 * @param msg CAN_receive_data 话题消息
 */
void chatterCallback(const ros_car::VCU_frame::ConstPtr& msg)
{
  if(DEF_RECEIVE_ID == msg->uID || DEF_SEND_ID == msg->uID)
  {
    ROS_INFO("CAN_frame_data is as follow :");
    ROS_INFO("TimeFlag is \t%d",msg->uTimeFlag);
    ROS_INFO("nSendType is \t%d",msg->nSendType);
    ROS_INFO("bRemoteFlag is \t%d",msg->bRemoteFlag);
    ROS_INFO("bExternFlag is \t%d",msg->bExternFlag);
    ROS_INFO("nDataLen is \t%d",msg->nDataLen);
    ROS_INFO("uID is \t\t0x%x",msg->uID);

    VCU_FRAME_DATA VCU_FRAME_DATA;
    memcpy(&VCU_FRAME_DATA, &(msg->arryData), sizeof(VCU_FRAME_DATA));
    ROS_INFO("DATA is as follow :");
    ROS_INFO(  "\nVCU_GEAR_REQ:\t[0x%x]\nVCU_NULL_1:\t[0x%x]\nVCU_BREAK_REQ:\t[0x%x]\nVCU_STR_REQ:\t[0x%x]\nVCU_PED_REQ:\t[0x%x]\nVCU_MODE:\t[0x%x]\n",
          VCU_FRAME_DATA.VCU_GEAR_REQ,
          VCU_FRAME_DATA.VCU_NULL_1,
          VCU_FRAME_DATA.VCU_BREAK_REQ,
          VCU_FRAME_DATA.VCU_STR_REQ,
          VCU_FRAME_DATA.VCU_PED_REQ,
          VCU_FRAME_DATA.VCU_MODE);
    ROS_INFO(  "\nVCU_NULL_2:\t[0x%x]\n",
          VCU_FRAME_DATA.VCU_NULL_2);
    ROS_INFO(  "\nVCU_PED_VALUE:\t[0x%x]\n",
          VCU_FRAME_DATA.VCU_PED_VALUE);
    ROS_INFO(  "\nVCU_STR_ANGLE_H:[0x%x]\n",
          VCU_FRAME_DATA.VCU_STR_ANGLE_H);
    ROS_INFO(  "\nVCU_NULL_3:\t[0x%x]\nVCU_STR_ANGLE_L:[0x%x]\n",
          VCU_FRAME_DATA.VCU_NULL_3,VCU_FRAME_DATA.VCU_STR_ANGLE_L);
    ROS_INFO(  "\nVCU_BREAK_VALUE:[0x%x]\n",
          VCU_FRAME_DATA.VCU_BREAK_VALUE);
    ROS_INFO(  "\nVCU_NULL_4:\t[0x%x]\nVCU_GEAR_VALUE:\t[0x%x]\n",
          VCU_FRAME_DATA.VCU_NULL_4,VCU_FRAME_DATA.VCU_GEAR_VALUE);
    ROS_INFO(  "\nVCU_NULL_5:\t[0x%x]\n",
          VCU_FRAME_DATA.VCU_NULL_5);
  }
}

/**
 * @brief CAN 监听节点
 * 
 * @param argc 
 * @param argv 
 * @return int 
 */
int main(int argc, char **argv)
{
  ros::init(argc, argv, "listener_CAN");

  ROS_INFO("This is listener_CAN");

  ros::NodeHandle n;

  ros::Subscriber receive = n.subscribe("CAN_receive_data", 1000, chatterCallback);

  ros::spin();

  return 0;
}
